import {
	get,
	post,
} from "@/common/js/http";
import {
	isSet
} from "@/common/js/helper";
let app = getApp();
let that = null;
export default {
	data() {
		return {
			CustomBar: this.CustomBar,
			isHidden: true,
			name: '',
			isLoad: true, // 修改这里，默认设置为true，确保组件可见
			firstVisit: true,
			listData: {},
			listInitial: {},
			listCur: '',
			listCurID: ''
		};
	},
	onLoad(options) {
		that = this;
		that.checkAgent(options);
		that.checkScene();
		if (that.checkFirst(options)) {
			that.getCityList();
		}
	},
	methods: {
		/**
		 * 检查场景信息
		 */
		checkScene() {
			let scene = app.globalData.scene
			if (scene == 0) {
				return
			}

			//#ifdef MP-WEIXIN
			uni.login({
				success: (res) => {
					post('user/scene', {
							code: res.code,
							scene: scene
						})
						.then((res) => {
							console.log(res);
						}).catch((res) => {
							console.log(res);
						});
				}
			});
			//#endif
		},

		/**
		 * 记录代理
		 * @param {Object} options
		 */
		checkAgent(options) {
			if (isSet(options.agent_id)) {
				let agent_id = options.agent_id;
				if (agent_id) {
					uni.login({
						success: (e) => {
							let code = e.code
							post('user/share', {
									code: code,
									agent_id: agent_id
								})
								.then(function(res) {
									console.log(res);
								});
						}
					});
				}
			}
		},

		/**
		 * 检查首次访问
		 */
		checkFirst(options) {
			let isNew = true; // 判断是否真正new

			let cacheFirstVisit = app.globalData.config.storage.getFristData();

			if (cacheFirstVisit == '') {
				cacheFirstVisit = 1;
			}

			let opt_first_visit = typeof options.first_visit != 'undefined' ? options.first_visit : -1;

			if (opt_first_visit == -1) {
				if (cacheFirstVisit > 1) {
					isNew = false;
				}
			} else {
				if (opt_first_visit > 1) {
					isNew = false;
				}
			}

			if (isNew == false) {
				uni.reLaunch({
					url: '../../index/index'
				});
				return false;
			}

			return isNew;
		},

		/**
		 * 获取城市列表
		 */
		getCityList() {
			get('region/get_group', {
				name: that.name
			}).then((res) => {
				console.log(res);
				that.isLoad = true;
				that.listData = res.data;
			}).catch((res) => {
				console.log(res);
			});
		},

		/**
		 * 搜索输入事件
		 */
		searchInputTap: function(options) {
			that.name = options.detail.value;
		},

		/**
		 * 搜索城市事件
		 */
		searchCityTap(options) {
			that.getCityList();
		},

		/**
		 * 选中城市事件
		 */
		cityTap(t) {
			let currentCity = t.currentTarget.dataset.data;
			app.globalData.config.storage.setCurrentCityData(currentCity);
			let examData = app.globalData.config.storage.getCurrentExamData();
			if (typeof examData.id != 'undefined') {
				uni.reLaunch({
					url: '../../index/index'
				});
			} else {
				uni.navigateTo({
					url: '../exam/exam'
				});
			}
		},


		getCur(e) {
			that.hidden = false;
			that.listCur = e.target.id;
		},

		setCur(e) {
			that.hidden = true;
			that.listCur = that.listCur
		},

		// 滑动选择Item
		tMove(e) {
			let y = e.touches[0].clientY;
			let offsettop = this.boxTop;
			let that = this; // 判断选择区域,只有在选择区才会生效
			if (y > offsettop) {
				let num = parseInt((y - offsettop) / 20);
				that.listCur = that.listData[num];
			}
		},

		//触发全部开始选择
		tStart() {
			that.hidden = false;
		},

		//触发结束选择
		tEnd() {
			that.hidden = true;
			that.listCurID = this.listCur;
		}
	}
};